/*
 * *
 *  * <pre>
 *  *     author : ${USER}
 *  *     e-mail : xxx@xx
 *  *     time   : ${YEAR}/${MONTH}/${DAY}
 *  *     desc   :
 *  *     version: 1.0
 *  * </pre>
 *
 */

package com.ovopark.firecar.okhttp;


import com.caoustc.okhttplib.okhttp.BaseHttpRequestCallback;
import com.caoustc.okhttplib.okhttp.Constants;
import com.caoustc.okhttplib.okhttp.OkHttpManager;
import com.caoustc.okhttplib.okhttp.OkHttpMethod;
import com.caoustc.okhttplib.okhttp.OkHttpRequestParams;
import com.socks.library.KLog;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class OkHttpRequest {
    private static final String TAG = "OkHttpRequest";

    public static final int REQ_TIMEOUT = 30 * 1000;

    public static void get(String url) {
        get(url, new OkHttpRequestParams(), null);
    }

    public static void get(String url, OkHttpRequestParams params) {
        get(url, params, null);
    }

    public static void get(String url, BaseHttpRequestCallback callback) {
        get(url, new OkHttpRequestParams(), callback);
    }

    public static void get(String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        get(url, params, REQ_TIMEOUT, callback);
    }

    public static void get(String parseString, boolean isCancel, Class dataClass, String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
//        get(parseString, isCancel, false, false, dataClass, url, params, REQ_TIMEOUT, callback);
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(REQ_TIMEOUT, TimeUnit.MILLISECONDS);
        builder.connectTimeout(REQ_TIMEOUT, TimeUnit.MILLISECONDS);
        builder.writeTimeout(REQ_TIMEOUT, TimeUnit.MILLISECONDS);
        executeRequest(parseString, isCancel, false, false, dataClass, OkHttpMethod.GET,
                url, params, builder, callback);
    }

    public static void get(String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        OkHttpToRetrofit.getInstance().getFormParseRequest(url, params, callback);
    }

    public static void get(String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(OkHttpMethod.GET, url, params, builder, callback);
    }

    public static void post(String url) {
        post(url, null, null);
    }

    public static void post(String url, OkHttpRequestParams params) {
        post(url, params, null);
    }

    public static void post(String url, BaseHttpRequestCallback callback) {
        post(url, null, callback);
    }

    public static void post(String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        post(url, params, REQ_TIMEOUT, callback);
    }

    public static void post(String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        OkHttpToRetrofit.getInstance().postFormParseRequest(url, params, callback);
    }

    public static void post(String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(OkHttpMethod.POST, url, params, builder, callback);
    }

    public static void put(String url) {
        put(url, null, null);
    }

    public static void put(String url, OkHttpRequestParams params) {
        put(url, params, null);
    }

    public static void put(String url, BaseHttpRequestCallback callback) {
        put(url, null, callback);
    }

    public static void put(String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        put(url, params, REQ_TIMEOUT, callback);
    }

    public static void put(String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
        executeRequest(OkHttpMethod.PUT, url, params, builder, callback);
    }

    public static void put(String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(OkHttpMethod.PUT, url, params, builder, callback);
    }

    public static void delete(String url) {
        delete(url, null, null);
    }

    public static void delete(String url, OkHttpRequestParams params) {
        delete(url, params, null);
    }

    public static void delete(String url, BaseHttpRequestCallback callback) {
        delete(url, null, callback);
    }

    public static void delete(String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        delete(url, params, REQ_TIMEOUT, callback);
    }

    public static void delete(String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
        executeRequest(OkHttpMethod.DELETE, url, params, builder, callback);
    }

    public static void delete(String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(OkHttpMethod.DELETE, url, params, builder, callback);
    }

    public static void head(String url) {
        head(url, null, null);
    }

    public static void head(String url, OkHttpRequestParams params) {
        head(url, params, null);
    }

    public static void head(String url, BaseHttpRequestCallback callback) {
        head(url, null, callback);
    }

    public static void head(String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        head(url, params, REQ_TIMEOUT, callback);
    }

    public static void head(String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
        executeRequest(OkHttpMethod.HEAD, url, params, builder, callback);
    }

    public static void head(String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(OkHttpMethod.HEAD, url, params, builder, callback);
    }

    public static void patch(String url) {
        patch(url, null, null);
    }

    public static void patch(String url, OkHttpRequestParams params) {
        patch(url, params, null);
    }

    public static void patch(String url, BaseHttpRequestCallback callback) {
        patch(url, null, callback);
    }

    public static void patch(String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        patch(url, params, REQ_TIMEOUT, callback);
    }

    public static void patch(String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
        executeRequest(OkHttpMethod.PATCH, url, params, builder, callback);
    }

    public static void patch(String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(OkHttpMethod.PATCH, url, params, builder, callback);
    }

    //是否要取消上一个请求
    public static void post(boolean isCancel, String url) {
        post(isCancel, url, null, null);
    }

    public static void post(boolean isCancel, String url, OkHttpRequestParams params) {
        post(isCancel, url, params, null);
    }

    public static void post(boolean isCancel, String url, BaseHttpRequestCallback callback) {
        post(isCancel, url, null, callback);
    }

    public static void post(boolean isCancel, String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        post(isCancel, url, params, REQ_TIMEOUT, callback);
    }

    public static void post(boolean isCancel, String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        post(null, isCancel, url, params, timeout, callback);
    }

    public static void post(String parseString, String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        post(parseString, true, url, params, REQ_TIMEOUT, callback);
    }

    public static void post(String parseString, String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        post(parseString, true, url, params, timeout, callback);
    }

    public static void post(String parseString, boolean isCancel, String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        post(parseString, isCancel, url, params, REQ_TIMEOUT, callback);
    }

    public static void post(String parseString, boolean isCancel, String url, OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        post(parseString, isCancel, false, false, null, url, params, timeout, callback);
    }

    public static void post(String parseString, boolean isCancel, Class dataClass, String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        post(parseString, isCancel, false, false, dataClass, url, params, REQ_TIMEOUT, callback);
    }

    public static void post(String parseString, boolean isCancel, boolean showOnStart, boolean showOnFailure, Class dataClass, String url, OkHttpRequestParams params, BaseHttpRequestCallback callback) {
        post(parseString, isCancel, showOnStart, showOnFailure, dataClass, url, params, REQ_TIMEOUT, callback);
    }

    public static void post(String parseString, boolean isCancel, boolean showOnStart, boolean showOnFailure,
                            Class dataClass, String url, OkHttpRequestParams params, long timeout,
                            BaseHttpRequestCallback callback) {
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
        executeRequest(parseString, isCancel, showOnStart, showOnFailure, dataClass, OkHttpMethod.POST,
                url, params, builder, callback);
    }

    public static void post(String parseKeyOne, String parseKeyTwo, boolean isCancel, boolean showOnStart, boolean showOnFailure,
                            Class dataClass, String url, OkHttpRequestParams params, long timeout,
                            BaseHttpRequestCallback callback) {
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
        executeRequest(parseKeyOne, parseKeyTwo, isCancel, showOnStart, showOnFailure, dataClass, OkHttpMethod.POST,
                url, params, builder, callback);
    }

    public static void execute(OkHttpMethod method, String parseKeyOne, String parseKeyTwo, boolean isCancel,
                               boolean showOnStart, boolean showOnFailure, Class dataClass, String url,
                               OkHttpRequestParams params, long timeout, BaseHttpRequestCallback callback) {
        OkHttpClient.Builder builder = OkHttpManager.getInstance().getOkHttpClientBuilder();
        builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
        builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
        executeRequest(parseKeyOne, parseKeyTwo, isCancel, showOnStart, showOnFailure, dataClass, method,
                url, params, builder, callback);
    }

    /**
     * 定点取消.
     *
     * @param url
     */
    public static void cancelUrl(String url) {
        KLog.i(TAG, "cacel url :" + url);
        RequestSender requestSender = RequestSender.getInstance();
        requestSender.stop(url);
    }

    /**
     * 全部请求都销毁.
     */
    public static void cancelAll() {
        KLog.i(TAG, "cacel all ()!");
        RequestSender requestSender = RequestSender.getInstance();
        requestSender.stop();
    }


//    public static void download(String url, File target) {
//        download(url, target, null);
//    }
//
//    public static void download(String url, File target, FileDownloadCallback callback) {
//        if (!StringUtils.isEmpty(url) && target != null) {
//            FileDownloadTask task = new FileDownloadTask(url, target, callback);
//            task.execute();
//        }
//    }

    private static void executeRequest(OkHttpMethod method, String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(null, true, method, url, params, builder, callback);
    }

    private static void executeRequest(String parseString, OkHttpMethod method, String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(parseString, true, method, url, params, builder, callback);
    }

    private static void executeRequest(String parseString, boolean isCancel, OkHttpMethod method, String url, OkHttpRequestParams params, OkHttpClient.Builder builder, BaseHttpRequestCallback callback) {
        executeRequest(parseString, isCancel, false, false, null, method, url, params, builder, callback);
    }

    private static void executeRequest(String parseString, boolean isCancel, boolean showOnStart,
                                       boolean showOnFailure, Class dataClass, OkHttpMethod method,
                                       String url, OkHttpRequestParams params, OkHttpClient.Builder builder,
                                       BaseHttpRequestCallback callback) {
        executeRequest(null, parseString, isCancel, showOnStart, showOnFailure, dataClass, method, url, params, builder, callback);
    }

    private static void executeRequest(String parseKeyOne, String parseKeyTwo, boolean isCancel, boolean showOnStart,
                                       boolean showOnFailure, Class dataClass, OkHttpMethod method,
                                       String url, OkHttpRequestParams params, OkHttpClient.Builder builder,
                                       BaseHttpRequestCallback callback) {

        if (url == null) {
            if (callback != null) {
                callback.onFailure(Constants.ERROR_RESPONSE_NULL, "url is empty !");
            }
            return;
        }

        //get request .
        if (method == OkHttpMethod.GET) {
            KLog.i(TAG, "executeRequest GET ~" + url);
            OkHttpToRetrofit.getInstance()
                    .setParseKeyTwo(parseKeyOne, parseKeyTwo)
                    .getFormParseRequest(url, params, dataClass, callback);
        } else if (method == OkHttpMethod.PUT) {//put request .
            KLog.i(TAG, "executeRequest PUT ~" + url);
            OkHttpToRetrofit.getInstance()
                    .setParseKeyTwo(parseKeyOne, parseKeyTwo)
                    .putFormParseRequest(url, params, dataClass, callback);
        } else {//post request .
            KLog.i(TAG, "executeRequest POST ~" + url);
            OkHttpToRetrofit.getInstance()
                    .setParseKeyTwo(parseKeyOne, parseKeyTwo)
                    .postFormParseRequest(url, params, dataClass, callback);
        }
    }
}
